package com.bit.book.advice;

import com.bit.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
@ResponseBody//这个注释一定要加，不加返回的就是页面 会出错
public class ResponseExceptionHandle {
//    @ExceptionHandler
//    public Result exceptionHandler(Exception e){
//        log.error("发生异常，e:",e);
//        return Result.fail("内部错误，e: " + e);
//    }
//    @ExceptionHandler
//    public Result exceptionHandler(NullPointerException e){
//        log.error("空指针异常, e:",e);
//        return Result.fail("空指针异常，e:"+ e);
//    }
//    @ExceptionHandler
//    public Result exceptionHandler(ArithmeticException e){
//        log.error("算术运算异常,e:",e);
//        return Result.fail("算术运算异常，e:"+ e);
//    }
//    @ExceptionHandler
//    public Result exceptionHandler(IndexOutOfBoundsException e){
//        log.error("数组越界异常,e:",e);
//        return Result.fail("数组越界异常，e:"+ e);
//    }

    //    //两种方式

    //第二种
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler1(Exception e){
        log.error("发生异常，e:",e);
        return Result.fail("内部错误，e: " + e);
    }
    @ExceptionHandler(NullPointerException.class)
    public Result exceptionHandler2(Exception e){
        log.error("空指针异常, e:",e);
        return Result.fail("空指针异常，e:"+ e);
    }
    @ExceptionHandler(ArithmeticException.class)
    public Result exceptionHandler3(Exception e){
        log.error("算术运算异常,e:",e);
        return Result.fail("算术运算异常，e:"+ e);
    }
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result exceptionHandler4(Exception e){
        log.error("数组越界异常,e:",e);
        return Result.fail("数组越界异常，e:"+ e);
    }
}
